﻿using Castle.DynamicProxy;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XsqTech.SmartOA.Infrastructure.Filter.Blockers
{
    public class ServiceLoggerInterceptor : IInterceptor
    {
        private readonly ILogger<ServiceLoggerInterceptor> _logger;

        public ServiceLoggerInterceptor(ILogger<ServiceLoggerInterceptor> logger)
        {
            _logger = logger;
        }

        public void Intercept(IInvocation invocation)
        {
            Stopwatch stopwatch = new Stopwatch();
            //启动服务
            stopwatch.Start();
            invocation.Proceed();
            //可关服务
            stopwatch.Stop();
            string requestUrl = invocation.Method.Name;
            string className = invocation.TargetType.Name;
            //控制台窗口输出毫秒
            _logger.LogInformation($"执行了{className}的{requestUrl}，消耗了{stopwatch.ElapsedMilliseconds} 毫秒，执行结果是{invocation.ReturnValue}");
        }
    }
}
